unit tagedit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Buttons, ComCtrls, Grids, ExtDlgs;

type

  TAttribute = (taNone, taID, taClass, taHref, taStyle, taOnclick);

  TTagAttribute = record
    Attribute: TAttribute;
    Name: string;
    Value: string;
  end;

  TTagInfo = record
    Tag: string;
    Attributes: array of TTagAttribute;
  end;

  { TTagEditor }

  TTagEditor = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Button1: TButton;
    Button2: TButton;
    class_Edit: TEdit;
    border_Edit: TEdit;
    Label18: TLabel;
    onkeydown_Edit: TEdit;
    OpenPictureDialog1: TOpenPictureDialog;
    type_ComboBox: TComboBox;
    src_Edit1: TEdit;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    onmouseover_Edit: TEdit;
    onmousemove_Edit: TEdit;
    onmouseout_Edit: TEdit;
    src_Edit: TEdit;
    alt_Edit: TEdit;
    ScriptPage: TTabSheet;
    width_Edit: TEdit;
    Label10: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    onclick_Edit: TEdit;
    Label7: TLabel;
    CustomGrid: TStringGrid;
    OtherPage: TTabSheet;
    img_page: TTabSheet;
    target_ComboBox: TComboBox;
    name_Edit: TEdit;
    href_Edit: TEdit;
    id_Edit: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    PageControl: TPageControl;
    Panel1: TPanel;
    a_Page: TTabSheet;
    CommonPage: TTabSheet;
    EventsPage: TTabSheet;
    style_Edit: TEdit;
    height_Edit: TEdit;
    procedure ComboBoxSelect(Sender: TObject);
    procedure SrcButtonClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    procedure SetTag( aTag: TTagInfo );
    function GetTag: string;
  end;


var
  TagEditor: TTagEditor;


function TrimTag( aTag: string ): string;
Function PosFrom (c : Char; Const s : AnsiString; startpoint: Integer) : SizeInt;

implementation

Function PosFrom (c : Char; Const s : AnsiString; startpoint: Integer) : SizeInt;
var
  i: SizeInt;
  pc : pchar;
begin
  pc:=@s[startpoint];
  for i:=startpoint to length(s) do
   begin
     if pc^=c then
      begin
        posFrom:=i;
        exit;
      end;
     inc(pc);
   end;
  posFrom:=0;
end;


function TrimTag(aTag: string): string;
var
  p: integer;
begin
  p := 1;
  while ((p<Length(aTag)) and (aTag[p] in [#32, #34, #39])) do
   Inc(p);
  if p< Length(aTag) then Delete(aTag, 1, p-1);
  p := Length(aTag);
  while ((p>0) and (aTag[p] in [#32, #34, #39])) do
   Dec(p);
  if p>0 then Delete(aTag, p+1, MaxInt);
  Result := aTag;
end;

{$R *.lfm}

{ TTagEditor }

procedure TTagEditor.ComboBoxSelect(Sender: TObject);
begin
  with Sender as TComboBox do
     begin
       Text := Items[ItemIndex];
     end;
end;

procedure TTagEditor.SrcButtonClick(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
     with TButton(Sender).AnchorSide[akTop].Control as TEdit do
        begin
          Text := OpenPictureDialog1.FileName;
        end;
end;

procedure TTagEditor.SetTag(aTag: TTagInfo);
var
  customtagcount: Integer;
  c: Integer;
  x: Integer;
  cc: TComponent;
  p: SizeInt;
  tagboxfound: Boolean;
  procedure SetOptionalPages(t: string);
  var
    sox: integer;
  begin
    sox := 0;
    while (sox < ComponentCount) do
     begin
       if Components[sox] is TTabSheet then
          if Pos(t + '_', Components[sox].Name)=1 then
             begin
               TTabSheet(Components[sox]).TabVisible := true;
               PageControl.ActivePage := TTabSheet(Components[sox]);
             end;
       Inc(sox);
     end;
  end;
begin
  Caption := Caption + '<' + aTag.Tag + '>';
  customtagcount := 0;
  CustomGrid.RowCount := customtagcount;
  SetOptionalPages(aTag.Tag);
  for x := 0 to Length(aTag.Attributes)-1 do
     begin
       c := 0;
       tagboxfound := false;
       while (c < ComponentCount) and (not tagboxfound) do // search for input box
          begin
            cc := TComponent(Components[c]);
            p := Pos(aTag.Attributes[x].Name + '_', cc.Name);
            if (p=1) and TTabSheet(TWinControl(cc).Parent).TabVisible then
               begin
                 if (cc is TEdit) then TEdit(cc).Text := aTag.Attributes[x].Value
                 else if (cc is TComboBox) then TComboBox(cc).Text := aTag.Attributes[x].Value;
                 tagboxfound := true; //abort search for input box
               end
            else Inc(c);
          end;
       if not tagboxfound then
          begin
            Inc(customtagcount);
            CustomGrid.RowCount := customtagcount+1;
            CustomGrid.Cells[0, customtagcount] := aTag.Attributes[x].Name;
            CustomGrid.Cells[1, customtagcount] := aTag.Attributes[x].Value;
          end
     end;
end;

function TTagEditor.GetTag: string;
var
  p: SizeInt;
  c: Integer;
  cc: TComponent;
  n: String;
  v: String;
begin
  p := Pos('<', Caption);
  Result := Copy(Caption, p, Length(Caption)-p);
  for c := 0 to ComponentCount-1 do
   begin
     cc := TComponent(Components[c]);
     p := pos('_', cc.Name);
     if p>0 then
        if TTabSheet(TWinControl(cc).Parent).TabVisible then
           begin
             if cc is TEdit then
               begin
                 if Trim(TEdit(cc).Text) <> '' then
                    Result := Result + #32 + Copy(cc.Name, 1, p-1) + '="' + TEdit(cc).Text + '"';
               end
             else if cc is TComboBox then
                begin
                  if Trim(TComboBox(cc).Text) <> '' then
                     Result := Result + #32 + Copy(cc.Name, 1, p-1) + '="' + TComboBox(cc).Text + '"';
                end;
           end;
   end;
  for c := 1 to CustomGrid.RowCount-1 do
   begin
     n := Trim(CustomGrid.Cells[0, c]);
     v := Trim(CustomGrid.Cells[1, c]);
     if (n<>'') and (v<>'')
        then Result := Result + #32 + n + '="' + v + '"';
   end;
   Result := Result+'>';
end;

end.

